﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using PwdMgr.PwdMgrService;
using PwdMgr.PwdMgrService.DataService;

namespace PwdMgrUI.Objects
{
    public class SearchThread
    {
        public event Action<object> SearchCompleted;
        public event Action SearchStarted;

        private IDataService _dataService = DataServiceFactory.CreateInstance();
        private Timer _timer;
        private string _searchText;

        public void QuickSearch(string searchText)
        {
            if (_timer == null)
                _timer = new Timer(Timer_Tick);

            _searchText = searchText;
            _timer.Change(1000, Timeout.Infinite);
        }

        void Timer_Tick(object sender)
        {
            if (SearchStarted != null)
                SearchStarted();

            var session = App.GetSession();
            var result = _dataService.GetSensitiveInfo(_searchText, session.SessionID);

            if (SearchCompleted != null)
                SearchCompleted(result);
        }
    }
}
